package auth

import (
	"context"
	"fmt"
	"github.com/spf13/cast"
	"github.com/tidwall/gjson"
	"insighttracking/apps/sorting/internal/svc"
	myerror "insighttracking/common/error"
	"net/http"
)

type AccountInfoVo struct {
	AccountId    int64  `json:"accountId"`
	Username     string `json:"username"`
	AccountType  int64  `json:"accountType"`
	EnterpriseId int64  `json:"enterpriseId"`
	CustomerId   int64  `json:"customerId"`
	PersonId     int64  `json:"personId"`
	PersonName   string `json:"personName"`
}

func GetAccountInfo(svcCtx *svc.ServiceContext, request *http.Request) (AccountInfoVo, error) {
	token := request.Header.Get("token")
	redisData, err := svcCtx.RedisDb.Get(context.Background(), token).Result()
	if err != nil {
		return AccountInfoVo{}, myerror.TokenExpired
	}
	fmt.Println(redisData, err, "redis中读取的数据")
	accountId := gjson.Get(redisData, "accountId").String()
	username := gjson.Get(redisData, "username").String()
	accountType := gjson.Get(redisData, "accountType").String()
	enterpriseId := gjson.Get(redisData, "enterpriseId").String() // 企业id
	customerId := gjson.Get(redisData, "customerId").String()     // 门店id
	personId := gjson.Get(redisData, "personId").String()         // 门店id
	personName := gjson.Get(redisData, "personName").String()     // 门店id
	return AccountInfoVo{
		AccountId:    cast.ToInt64(accountId),
		Username:     username,
		AccountType:  cast.ToInt64(accountType),
		EnterpriseId: cast.ToInt64(enterpriseId),
		CustomerId:   cast.ToInt64(customerId),
		PersonId:     cast.ToInt64(personId),
		PersonName:   personName,
	}, nil
}
